#
# @lc app=leetcode.cn id=107 lang=python3
#
# [107] 二叉树的层次遍历 II
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        res = []
        tmp = [root]
        while tmp:
            tmp_layer_val = []
            next_layer_node = []
            for node in tmp:
                if node:
                    tmp_layer_val.append(node.val)
                    next_layer_node.extend([node.left, node.right])
            if tmp_layer_val:
                res.insert(0, tmp_layer_val)
            tmp = next_layer_node
        return res
# @lc code=end

